<?php
class wechatCallbackapiTest {
	// 验证签名
	public function valid() {
		$echoStr = $_GET ["echostr"];
		$signature = $_GET ["signature"];
		$timestamp = $_GET ["timestamp"];
		$nonce = $_GET ["nonce"];
		$token = TOKEN;
		$tmpArr = array (
				$token,
				$timestamp,
				$nonce 
		);
		sort ( $tmpArr, SORT_STRING );
		$tmpStr = implode ( $tmpArr );
		$tmpStr = sha1 ( $tmpStr );
		if ($tmpStr == $signature) {
			echo $echoStr;
			exit ();
		}
	}
	
	// 响应消息
	public function responseMsg() {
		$postStr = $GLOBALS ["HTTP_RAW_POST_DATA"];
		if (! empty ( $postStr )) {
			$this->logger ( "R \r\n" . $postStr );
			$postObj = simplexml_load_string ( $postStr, 'SimpleXMLElement', LIBXML_NOCDATA );
			$RX_TYPE = trim ( $postObj->MsgType );
			
			if (($postObj->MsgType == "event") && ($postObj->Event == "subscribe" || $postObj->Event == "unsubscribe")) {
				// 过滤关注和取消关注事件
			} else {
			}
			// 消息类型分离
			switch ($RX_TYPE) {
				case "event" :
					$result = $this->receiveEvent ( $postObj );
					break;
				case "text" :
					if (strstr ( $postObj->Content, "第三方" )) {
						$result = $this->relayPart3 ( "http://www.fangbei.org/test.php" . '?' . $_SERVER ['QUERY_STRING'], $postStr );
					} else {
						$result = $this->receiveText ( $postObj );
					}
					break;
				case "image" :
					$result = $this->receiveImage ( $postObj );
					break;
				case "location" :
					$result = $this->receiveLocation ( $postObj );
					break;
				case "voice" :
					$result = $this->receiveVoice ( $postObj );
					break;
				case "video" :
					$result = $this->receiveVideo ( $postObj );
					break;
				case "link" :
					$result = $this->receiveLink ( $postObj );
					break;
				default :
					$result = "unknown msg type: " . $RX_TYPE;
					break;
			}
			echo $result;
		} else {
			echo "";
			exit ();
		}
	}
	
	// 接收事件消息
	private function receiveEvent($object) {
		$content = "";
		switch ($object->Event) {
			case "subscribe" :
				$content = "感谢关注英泰君！mo-握手不同于普遍大街上大型连锁的流水线公式化发型店文化，我们隐于闹市中的小区内。致力于发型造型更生动更人性化及个性化的量身定制，集‘健康’‘环保’‘时尚’‘摩登’等理念于一体的温馨家庭式造型工作室，不只是发型设计，英泰给您的更是一种爱自己的生活态度。 您可致电0717-6851336/ 18271360313咨询服务！mo-爱心";
				$content .= (! empty ( $object->EventKey )) ? ("\n来自二维码场景 " . str_replace ( "qrscene_", "", $object->EventKey )) : "";
				break;
			case "unsubscribe" :
				$content = "取消关注";
				break;
			case "CLICK" :
				switch ($object->EventKey) {
					case "hufa" :
						$content [] = array (
								"Title" => "芳氛头皮护理系列—重塑健康秀发生长之基",
								"Description" => "提醒:点上方↑蓝色字“宜昌英泰发型私务所”即可关注英泰更多原创作品预约电话：0717-6851336灵感源于",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqjLJaQkoYQK851QhhJftAr2wS5gruUeJakRBc4ZH2IQtqH6vrDpB6vSHvWHt0vjicBXgSUD2WSsMKQ/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205790680&idx=1&sn=c00b73c98fda89650ae31525f63922ee#rd" 
						);
						$content [] = array (
								"Title" => "露蜜护色系列—长久保持染后发色魅力效果",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqiaDnGl3x4SXNJVKSSbvqGZ0jg7hSib1pFkCbdibn5zpYpiaDJCHlKsoq3WagpKaDjkdWgbVLjnwQohGw/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205790680&idx=2&sn=e97c0f5387dd06146044d6a5ef3d2507#rd" 
						);
						$content [] = array (
								"Title" => "丝亮顺滑系列—抑制毛躁营造丝亮顺滑",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqiaDnGl3x4SXNJVKSSbvqGZ0Ucq3ibPMKJl8F6UFndCp759jV7fm4ibic1BXAUcRosYNDCmx4IIlnyzHQ/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205790680&idx=3&sn=9ba1b4694eee9945b7fdc70a5ff26d20#rd" 
						);
						$content [] = array (
								"Title" => "水活修护系列—深层锁水还头发水润光泽",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqiaDnGl3x4SXNJVKSSbvqGZ0W14sf0icXqSxyUy5DVXZbq7coh989eyiaj6PRzYlPKw3tncicDUbABiaDg/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205790680&idx=4&sn=51657b998d9b9522fe8113f138a6ab4a#rd" 
						);
						$content [] = array (
								"Title" => "头皮生机系列—深层滋养健康育发防脱",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqiaDnGl3x4SXNJVKSSbvqGZ09weG7PDoa3PBAnwddzYkZNic6Joy1Kfl5ZMYDwsP5C2x27kicmXHFLJw/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205790680&idx=5&sn=9be73685bff76fcc0dac72604b3d4219#rd" 
						);
						$content [] = array (
								"Title" => "专业调理系统—清尘出世，焕活新生",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqiaDnGl3x4SXNJVKSSbvqGZ0droB9mXbuxQ1fpB6U2lctOuHEINvMLrlX3hNdtzm9Cz5YbXkqave5Q/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205790680&idx=6&sn=e1fa0024cdd1ddac346e562c014a9092#rd" 
						);
						break;
					case "ranfa" :
						$content [] = array (
								"Title" => "日本资生堂普盈丝染膏系列—发掘自身崭新内在魅力",
								"Description" => "提醒:点上方↑蓝色字“宜昌英泰发型私务所”即可关注英泰更多原创作品预约电话：0717-6851336PRIM",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqjLJaQkoYQK851QhhJftAr26njicHoWRibnteEd6td3xSgedfL0micZqdJ2hzUmR5MjbBhc8Y7h3yD2w/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205830793&idx=1&sn=91544fee5adf4b55898af569de4a7138&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "韩国原装艾尔芭ERBA专业染膏",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqjLJaQkoYQK851QhhJftAr28T7fWzibok8Vv5IkGbaH5DPH7lVELkf2icqK5t19Pe4TLdianszl8gQJw/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205830793&idx=2&sn=3b98d7e73e7381e2fb3d50caf818dbaa&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "韩国原装进口正品艾尔芭可婷安透喜安颜酸性护理",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqjLJaQkoYQK851QhhJftAr2qbVHckqYmQF3q6gibcrBhdukePpheWaMWTMEOImTmVDF1wzp16iaTw1Q/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205830793&idx=3&sn=ea861303f4218d79c5d0453689104d26&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "韩国进口PEARL紫水晶头发色彩打蜡酸性护理亮发因子",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqjLJaQkoYQK851QhhJftAr2dtcTFwAwBz1kU1yHfq1VkajzAIHr9FBxvssVjk3ndlL95Q31y9hLew/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205830793&idx=4&sn=e56970a408eb8c51301897de0eedd296&scene=18#rd" 
						);
						break;
					case "tangfa" :
						$content [] = array (
								"Title" => "资生堂瑰美特热烫塑型系列—随心塑造卷曲",
								"Description" => "提醒:点上方↑蓝色字“宜昌英泰发型私务所”即可关注英泰更多原创作品预约电话：0717-6851336瑰美特热",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5CwHK1Qbia4f5POHyJEiaicjbULgnZI6icc4Rl9sgLQQCFaiaJfkbAszfo7eww/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205832965&idx=1&sn=4f57fb44c25c2a4de07b37e6f0274f5f&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "资生堂柔亮光润水晶直烫液",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5CwgBKP44Fs129IBzor37lP6PdDdD8cILmddIdbicmp52MViaaQ6tqYCrWQ/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205832965&idx=2&sn=7f8ad94f213cfcf7c5e68348dd01261a&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "资生堂艾莉灵俏冷烫系列",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5CwDAUW0AiamHwO9Iib4tKJ1gktezLJuGFtOkib7D1kMuhzsm3f6AyEgD9oA/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205832965&idx=3&sn=a69cc5a95f3d99f3859982589bc8568f&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "韩国艾尔芭ECSS完美万能烫发水",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5CwEoduDddL9kdz3bRbaD782gVWV2CLsx3P9JJwe7cpib2AUc1tRVRRsbw/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205832965&idx=4&sn=49abf470b4689073320156d707d76f61&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "韩国ERBA艾尔芭Q冷烫发水",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5Cwu12CrHPsiaLkstKW0n3WzB62qciaQiaBAcCLj4KUvPg6lxeMIZOticoDEw/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205832965&idx=5&sn=10ba5c1e5f04f2e2fe36df63972ca7c4&scene=18#rd" 
						);
						break;
					case "zaoxing" :
						$content [] = array (
								"Title" => "秀场革命系列-“发妆”概念引领新一代造型革命",
								"Description" => "提醒:点上方↑蓝色字“宜昌英泰发型私务所”即可关注英泰更多原创作品预约电话：0717-6851336扫我关注",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5CwfcLS8RmnDBhP8xy9DwgbJA72sHdjuUWVHTgb50kPY5rJfcr0HHOo5A/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205836571&idx=1&sn=acb03184ae63ed4995e99ede76f3960b&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "造型打底—秀场造型蓬松隔离水、柔顺打底露",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5CwkibxOichIHKyZAfWYeFlVbewuoZjgmmYxHZFqoRrFM9YKAiaCYPazUGRQ/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205836571&idx=2&sn=9073afa1374435c8ccd598b25b67e033&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "基础造型—自然发泥、高定型力发泥",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5CwTJPUTdxp6ZxNYLBneXQTCC24Cel0wc4NeCx2OCAlPBSk5v7N9MF5Xw/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205836571&idx=3&sn=bc82bfe20342e898467a367bcdf131b7&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "基础造型—束感喷雾、塑型啫喱、丝质喷雾",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5Cw2qibB2iakjicyTjBXe2jSHXsSoVEkMnJNcKf2P3vTwyeG7mG2F1m1OAHw/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205836571&idx=4&sn=34bba8105ad33544dff93e8f93275198&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "自由随心造型—Powder Shake 雪绒蓬蓬粉雾",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5Cwf58x7MflNytUGcEBrM0PvOfjx22ZAlyOMpyWMAiajLx6Hy8fAPZHvtA/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205836571&idx=5&sn=18155c03c643261636e2ce42e6670406&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "韩国进口造型品艾尔芭系列",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5Cwzq8aKbIGWMyevLnHU8q2L7Y9uACO0BUxq0XWveJJ4iaJsNKtuIZNOPg/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205836571&idx=7&sn=ba8c41ee8a434986ebcbadc280dc77ba&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "韩国艾尔芭 免洗干枯毛躁护发精油",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5CwqAy23qAiaJcUdibGBOMhTA3ibrFyrZYYFsx45RVw185icsQPofG4ibSDwHg/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205836571&idx=7&sn=ba8c41ee8a434986ebcbadc280dc77ba&scene=18#rd" 
						);
						break;
					case "shishang" :
						$content [] = array (
								"Title" => "8款超级实用中长日系扎发（英泰力荐）",
								"Description" => "甜美的日系扎发，相信很多女生都会喜欢的，下面看看英泰君推荐的这几款扎发发型，约会再也不怕找不到合适的发型了。",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5CwZY1884JPx8Cmxp0Sc1o9qzWhv8UorS63kavnjNxMs8bpLGmb7h7K0A/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=206043296&idx=1&sn=28b38e320b2ac6942d2aa3a1048a423d&scene=18#rd" 
						);
						break;
					case "yuanchuang" :
						$content [] = array (
								"Title" => "原创是一种坚持",
								"Description" => "提醒:点上方↑蓝色字“宜昌英泰发型私务所”即可关注英泰更多原创作品预约电话：0717-6851336模特不是",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqiagkIfYMzWcN4UOdQVWNtibVgKoFQvjWlrygSYDw9883agrVLEBQdpWvN1HxQUcF7QrnHfS1VTkOTw/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205875094&idx=1&sn=a2b718e7a493ec695c83de106a8d8a8d&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "灰色太美，可是咱们手贱染过黑油打过红蜡",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqiagkIfYMzWcN4UOdQVWNtibVQvcXp7xVYoFz4M2CyKUNZLZh22Zibwa3kOzdMFeA5xesFEvocCYxWWA/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205875094&idx=2&sn=979af7c7de7d06225cb609560d5e7823&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "闪瞎，一秒让头发抛光",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqiagkIfYMzWcN4UOdQVWNtibVoatB7f67H12GIibGf5DMa1pKzDhVib4ycNYYgHLwImxKPT0s2muEQ09Q/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205875094&idx=3&sn=8d0f76053d5cfc2086ddc0028878d4d9&scene=18#rd" 
						);
						break;
					case "wuhen" :
						$content [] = array (
								"Title" => "做最适合你的‘无痕接发’",
								"Description" => "提醒:点上方↑蓝色字“宜昌英泰发型私务所”即可关注英泰更多原创作品预约电话：0717-6851336无痕接发",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqjZk5JeCFyLs8tX5US1D2qb87uIoxlmfPreiaJ37N8Jaxm5jia2QMibo2mlzJDtHwshGibAR0Pwgc1icEQ/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205876734&idx=1&sn=fe77e4cc687294d5c59efee4af4edb73&scene=18#rd" 
						);
						$content [] = array (
								"Title" => "那天的灰其实不夸张",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqjZk5JeCFyLs8tX5US1D2qbMVNcW3jVYce55iacictk9YCxFUX5O60ic0sESE3k3rictmibx8PunMGJheg/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205876734&idx=2&sn=83d14ea9cf772f5a1686d1ac282c8fdd&scene=18#rd" 
						);
						break;
					case "zuopin" :
						$content [] = array (
								"Title" => "眉上刘海能够怎么搭？",
								"Description" => "谁说女孩子一定要长发飘飘？！今天就为短发美眉平反。推荐1款热发型，让短发MM照样美丽百变，甜蜜可人",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqgKIpN4p0ibqLTQg1ib8gr5CwrO1ptwp9crbuOLN3OExJqIQ2GeHh74xwAzfgGibRx2iaKqVojT05FmNg/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=206063070&idx=1&sn=e19e23329e8d6210524304fb0e50c726&scene=18#rd" 
						);
						break;
					case "zhaodao" :
						$content [] = array (
								"Title" => "我们在这里",
								"Description" => "宜昌市西陵区西陵二路44号富丽小区2栋4单元201室 电话6851336",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqje6yTCeweobX68pjiaCj7j2dEYTHxlnUA2LchQxicSobJyG8unwylwU5jh9KPUKS7KjNjscp0GIia0w/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205901596&idx=1&sn=aa6f8e82a3018c4e2b8cbbe3fb58452c&scene=18#rd" 
						);
						break;
					case "faxingshi" :
						$content [] = array (
								"Title" => "匠人匠心-英泰成员简介",
								"Description" => "提醒:点上方↑蓝色字“宜昌英泰发型私务所”即可关注英泰更多原创作品预约电话：0717-6851336英泰发型",
								"PicUrl" => "https://mmbiz.qlogo.cn/mmbiz/VwjdEke2jqjLJaQkoYQK851QhhJftAr2ZsTnjvVYs9sW3OFCe6me6MWZFj2cowHYl77rp2RToNpPj7H9TwFukg/0?wx_fmt=jpeg",
								"Url" => "http://mp.weixin.qq.com/s?__biz=MjM5MDEwMzI0Mg==&mid=205961057&idx=1&sn=127a5f6e410d2b4fbc502d3c4f54b672&scene=18#rd" 
						);
						break;
					case "website" :
						$content [] = array (
								"Title" => "英泰发型私务所",
								"Description" => "官方网站",
								"PicUrl" => "",
								"Url" => "http://{$_SERVER['HTTP_HOST']}" . U ( "Index/index?openid={$object->FromUserName}" ) 
						);
						break;
					default :
						$content = "点击菜单：" . $object->EventKey;
						break;
				}
				break;
			case "VIEW" :
				$content = "跳转链接 " . $object->EventKey;
				break;
			case "SCAN" :
				$content = "扫描场景 " . $object->EventKey;
				break;
			case "LOCATION" :
				$content = "上传位置：纬度 " . $object->Latitude . ";经度 " . $object->Longitude;
				break;
			case "scancode_waitmsg" :
				if ($object->ScanCodeInfo->ScanType == "qrcode") {
					$content = "扫码带提示：类型 二维码 结果：" . $object->ScanCodeInfo->ScanResult;
				} else if ($object->ScanCodeInfo->ScanType == "barcode") {
					$codeinfo = explode ( ",", strval ( $object->ScanCodeInfo->ScanResult ) );
					$codeValue = $codeinfo [1];
					$content = "扫码带提示：类型 条形码 结果：" . $codeValue;
				} else {
					$content = "扫码带提示：类型 " . $object->ScanCodeInfo->ScanType . " 结果：" . $object->ScanCodeInfo->ScanResult;
				}
				break;
			case "scancode_push" :
				$content = "扫码推事件";
				break;
			case "pic_sysphoto" :
				$content = "系统拍照";
				break;
			case "pic_weixin" :
				$content = "相册发图：数量 " . $object->SendPicsInfo->Count;
				break;
			case "pic_photo_or_album" :
				$content = "拍照或者相册：数量 " . $object->SendPicsInfo->Count;
				break;
			case "location_select" :
				$content = "发送位置：标签 " . $object->SendLocationInfo->Label;
				break;
			default :
				$content = "receive a new event: " . $object->Event;
				break;
		}
		
		if (is_array ( $content )) {
			if (isset ( $content [0] ['PicUrl'] )) {
				$result = $this->transmitNews ( $object, $content );
			} else if (isset ( $content ['MusicUrl'] )) {
				$result = $this->transmitMusic ( $object, $content );
			}
		} else {
			$result = $this->transmitText ( $object, $content );
		}
		return $result;
	}
	
	// 接收文本消息
	private function receiveText($object) {
		$keyword = trim ( $object->Content );
		// 多客服人工回复模式
		if (strstr ( $keyword, "请问在吗" ) || strstr ( $keyword, "在线客服" )) {
			$result = $this->transmitService ( $object );
			return $result;
		}
		
		// 自动回复模式
		if (strstr ( $keyword, "文本" )) {
			$content = "这是个文本消息";
		} else if (strstr ( $keyword, "表情" )) {
			$content = "中国：" . $this->bytes_to_emoji ( 0x1F1E8 ) . $this->bytes_to_emoji ( 0x1F1F3 ) . "\n仙人掌：" . $this->bytes_to_emoji ( 0x1F335 );
		} else if (strstr ( $keyword, "单图文" )) {
			$content = array ();
			$content [] = array (
					"Title" => "单图文标题",
					"Description" => "单图文内容",
					"PicUrl" => "http://discuz.comli.com/weixin/weather/icon/cartoon.jpg",
					"Url" => "http://m.cnblogs.com/?u=txw1958" 
			);
		} else if (strstr ( $keyword, "图文" ) || strstr ( $keyword, "多图文" )) {
			$content = array ();
			$content [] = array (
					"Title" => "多图文1标题",
					"Description" => "",
					"PicUrl" => "http://discuz.comli.com/weixin/weather/icon/cartoon.jpg",
					"Url" => "http://m.cnblogs.com/?u=txw1958" 
			);
			$content [] = array (
					"Title" => "多图文2标题",
					"Description" => "",
					"PicUrl" => "http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg",
					"Url" => "http://m.cnblogs.com/?u=txw1958" 
			);
			$content [] = array (
					"Title" => "多图文3标题",
					"Description" => "",
					"PicUrl" => "http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg",
					"Url" => "http://m.cnblogs.com/?u=txw1958" 
			);
		} else if (strstr ( $keyword, "音乐" )) {
			$content = array ();
			$content = array (
					"Title" => "最炫民族风",
					"Description" => "歌手：凤凰传奇",
					"MusicUrl" => "http://121.199.4.61/music/zxmzf.mp3",
					"HQMusicUrl" => "http://121.199.4.61/music/zxmzf.mp3" 
			);
		} else {
// 			$content = date ( "Y-m-d H:i:s", time () ) . "\nOpenID：" . $object->FromUserName . "\n技术支持 方倍工作室";
$content='';
		}
		
		if (is_array ( $content )) {
			if (isset ( $content [0] )) {
				$result = $this->transmitNews ( $object, $content );
			} else if (isset ( $content ['MusicUrl'] )) {
				$result = $this->transmitMusic ( $object, $content );
			}
		} else {
			$result = $this->transmitText ( $object, $content );
		}
		return $result;
	}
	
	// 接收图片消息
	private function receiveImage($object) {
		$content = array (
				"MediaId" => $object->MediaId 
		);
		$result = $this->transmitImage ( $object, $content );
		return $result;
	}
	
	// 接收位置消息
	private function receiveLocation($object) {
		$content = "你发送的是位置，经度为：" . $object->Location_Y . "；纬度为：" . $object->Location_X . "；缩放级别为：" . $object->Scale . "；位置为：" . $object->Label;
		$result = $this->transmitText ( $object, $content );
		return $result;
	}
	
	// 接收语音消息
	private function receiveVoice($object) {
		if (isset ( $object->Recognition ) && ! empty ( $object->Recognition )) {
			$content = "你刚才说的是：" . $object->Recognition;
			$result = $this->transmitText ( $object, $content );
		} else {
			$content = array (
					"MediaId" => $object->MediaId 
			);
			$result = $this->transmitVoice ( $object, $content );
		}
		return $result;
	}
	
	// 接收视频消息
	private function receiveVideo($object) {
		$content = array (
				"MediaId" => $object->MediaId,
				"ThumbMediaId" => $object->ThumbMediaId,
				"Title" => "",
				"Description" => "" 
		);
		$result = $this->transmitVideo ( $object, $content );
		return $result;
	}
	
	// 接收链接消息
	private function receiveLink($object) {
		$content = "你发送的是链接，标题为：" . $object->Title . "；内容为：" . $object->Description . "；链接地址为：" . $object->Url;
		$result = $this->transmitText ( $object, $content );
		return $result;
	}
	
	// 回复文本消息
	private function transmitText($object, $content) {
		if (! isset ( $content ) || empty ( $content )) {
			return "";
		}
		
		$xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[%s]]></Content>
</xml>";
		$result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time (), $content );
		
		return $result;
	}
	
	// 回复图文消息
	private function transmitNews($object, $newsArray) {
		if (! is_array ( $newsArray )) {
			return "";
		}
		$itemTpl = "        <item>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            <PicUrl><![CDATA[%s]]></PicUrl>
            <Url><![CDATA[%s]]></Url>
        </item>
";
		$item_str = "";
		foreach ( $newsArray as $item ) {
			$item_str .= sprintf ( $itemTpl, $item ['Title'], $item ['Description'], $item ['PicUrl'], $item ['Url'] );
		}
		$xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[news]]></MsgType>
    <ArticleCount>%s</ArticleCount>
    <Articles>
$item_str    </Articles>
</xml>";
		
		$result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time (), count ( $newsArray ) );
		return $result;
	}
	
	// 回复音乐消息
	private function transmitMusic($object, $musicArray) {
		if (! is_array ( $musicArray )) {
			return "";
		}
		$itemTpl = "<Music>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
        <MusicUrl><![CDATA[%s]]></MusicUrl>
        <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
    </Music>";
		
		$item_str = sprintf ( $itemTpl, $musicArray ['Title'], $musicArray ['Description'], $musicArray ['MusicUrl'], $musicArray ['HQMusicUrl'] );
		
		$xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[music]]></MsgType>
    $item_str
</xml>";
		
		$result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
		return $result;
	}
	
	// 回复图片消息
	private function transmitImage($object, $imageArray) {
		$itemTpl = "<Image>
        <MediaId><![CDATA[%s]]></MediaId>
    </Image>";
		
		$item_str = sprintf ( $itemTpl, $imageArray ['MediaId'] );
		
		$xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[image]]></MsgType>
    $item_str
</xml>";
		
		$result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
		return $result;
	}
	
	// 回复语音消息
	private function transmitVoice($object, $voiceArray) {
		$itemTpl = "<Voice>
        <MediaId><![CDATA[%s]]></MediaId>
    </Voice>";
		
		$item_str = sprintf ( $itemTpl, $voiceArray ['MediaId'] );
		$xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[voice]]></MsgType>
    $item_str
</xml>";
		
		$result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
		return $result;
	}
	
	// 回复视频消息
	private function transmitVideo($object, $videoArray) {
		$itemTpl = "<Video>
        <MediaId><![CDATA[%s]]></MediaId>
        <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
    </Video>";
		
		$item_str = sprintf ( $itemTpl, $videoArray ['MediaId'], $videoArray ['ThumbMediaId'], $videoArray ['Title'], $videoArray ['Description'] );
		
		$xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[video]]></MsgType>
    $item_str
</xml>";
		
		$result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
		return $result;
	}
	
	// 回复多客服消息
	private function transmitService($object) {
		$xmlTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[transfer_customer_service]]></MsgType>
</xml>";
		$result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
		return $result;
	}
	
	// 回复第三方接口消息
	private function relayPart3($url, $rawData) {
		$headers = array (
				"Content-Type: text/xml; charset=utf-8" 
		);
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
		curl_setopt ( $ch, CURLOPT_POST, 1 );
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $rawData );
		$output = curl_exec ( $ch );
		curl_close ( $ch );
		return $output;
	}
	
	// 字节转Emoji表情
	function bytes_to_emoji($cp) {
		if ($cp > 0x10000) { // 4 bytes
			return chr ( 0xF0 | (($cp & 0x1C0000) >> 18) ) . chr ( 0x80 | (($cp & 0x3F000) >> 12) ) . chr ( 0x80 | (($cp & 0xFC0) >> 6) ) . chr ( 0x80 | ($cp & 0x3F) );
		} else if ($cp > 0x800) { // 3 bytes
			return chr ( 0xE0 | (($cp & 0xF000) >> 12) ) . chr ( 0x80 | (($cp & 0xFC0) >> 6) ) . chr ( 0x80 | ($cp & 0x3F) );
		} else if ($cp > 0x80) { // 2 bytes
			return chr ( 0xC0 | (($cp & 0x7C0) >> 6) ) . chr ( 0x80 | ($cp & 0x3F) );
		} else { // 1 byte
			return chr ( $cp );
		}
	}
	
	// 日志记录
	private function logger($log_content) {
		if (isset ( $_SERVER ['HTTP_APPNAME'] )) { // SAE
			sae_set_display_errors ( false );
			sae_debug ( $log_content );
			sae_set_display_errors ( true );
		} else if ($_SERVER ['REMOTE_ADDR'] != "127.0.0.1") { // LOCAL
			$max_size = 1000000;
			$log_filename = "log.xml";
			if (file_exists ( $log_filename ) and (abs ( filesize ( $log_filename ) ) > $max_size)) {
				unlink ( $log_filename );
			}
			file_put_contents ( $log_filename, date ( 'Y-m-d H:i:s' ) . " " . $log_content . "\r\n", FILE_APPEND );
		}
	}
}